﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ShopBLL;
using ShopModel;
using System.Text.RegularExpressions;
using System.Text;

public partial class OrderDetail : BasePage
{
    #region PROPERTIES

    private string ShipmentSelectedValue
    {
        get
        {
            HtmlInputRadioButton radio = null;
            for (int i = 0; i < pnlPayments.Controls.Count; i++)
            {
                if (pnlPayments.Controls[i] is Panel)
                {
                    radio = pnlPayments.Controls[i].Controls[0] as HtmlInputRadioButton;
                    if (radio != null && radio.Checked)
                    {
                        return radio.Value;
                    }
                }
            }

            return string.Empty;
        }
    }

    #endregion PROPERTIES

    #region VARIABLES

    List<PaymentType> ActivePayments = new List<PaymentType>();
    List<PaymentType> ActiveOnlinePayments = new List<PaymentType>();

    #endregion VARIABLES

    #region METHODS

    protected void GetPaymentTypes()
    {
        ActivePayments.AddRange(OrderManager.Instance.GetPaymentTypes(true, true, CommonTools.CurrentLanguageID));

        foreach (PaymentType p in ActivePayments)
        {
            if (p.PaymentGroup == PaymentGroups.Online)
                ActiveOnlinePayments.Add(p);
        }

        foreach (PaymentType p in ActiveOnlinePayments)
        {
            ActivePayments.Remove(p);
        }
    }

    public bool ValidateUser()
    {
        if (ctrlCustomerData.mode == controls_ctrlCustomerData.ModeType.NewAccount)
        {
            if (ctrlCustomerData.Password.Length < Membership.Provider.MinRequiredPasswordLength)
            {
                ShowError(string.Format(Resources.Language.NewClient_PasswordInvalid, Membership.MinRequiredPasswordLength, Membership.MinRequiredNonAlphanumericCharacters));
                return false;
            }
            else if (Membership.GetUser(ctrlCustomerData.Email) != null)
            {
                ShowError(Resources.Language.NewClient_EmailDuplicate);
                return false;
            }
            else
            {
                if (string.IsNullOrEmpty(Membership.PasswordStrengthRegularExpression))
                {
                    int nonAlphanumericCount = 0;
                    foreach (char c in ctrlCustomerData.Password)
                    {
                        if (!char.IsLetterOrDigit(c))
                        {
                            nonAlphanumericCount++;
                        }
                    }

                    if (nonAlphanumericCount < Membership.Provider.MinRequiredNonAlphanumericCharacters)
                    {
                        ShowError(string.Format(Resources.Language.NewClient_PasswordInvalid, Membership.MinRequiredPasswordLength, Membership.MinRequiredNonAlphanumericCharacters));
                        return false;
                    }
                }
                else
                {
                    Regex passwordStrength = new Regex(Membership.PasswordStrengthRegularExpression);
                    if (!passwordStrength.IsMatch(ctrlCustomerData.Password))
                    {
                        return false;
                    }
                }
            }
        }
        return true;

    }

    private bool ValidateShipmentType()
    {
        string shipmentId = ShipmentSelectedValue;
        int paymentId = 0;
        if (!string.IsNullOrEmpty(shipmentId))
        {
            paymentId = int.Parse(rblPayments.SelectedValue);
            if (paymentId == 0)
            {
                paymentId = GetSelectedOnlinePayment();
            }

            if (OrderManager.Instance.CheckShipmentPossiblities(int.Parse(shipmentId), paymentId, ctrlCustomerData.CountryID))
            {
                return true;
            }
        }

        ShowError(Resources.Language.ErrorWrongShipmentType);
        return false;
    }

    private void SelectOnlinePaymentType(int paymentID)
    {
        foreach (RepeaterItem item in rptOnlinePayments.Items)
        {
            HiddenField hf = (HiddenField)item.FindControl("hfOP");
            if (int.Parse(hf.Value) == paymentID)
            {
                RadioButton rb = (RadioButton)item.FindControl("rbOP");
                rb.Checked = true;
                return;
            }
        }
    }

    private int GetSelectedOnlinePayment()
    {
        foreach (RepeaterItem item in rptOnlinePayments.Items)
        {
            RadioButton rb = (RadioButton)item.FindControl("rbOP");
            if (rb.Checked == true)
            {
                HiddenField hf = (HiddenField)item.FindControl("hfOP");
                return int.Parse(hf.Value);
            }
        }
        return -1;
    }

    private void AddAttributesAndScripts()
    {
        StringBuilder script = new StringBuilder();
        StringBuilder array = new StringBuilder();

        DropDownList ddl = (DropDownList)ctrlCustomerData.FindControl("ddlCountry");

        //ustawienie tablicy krajów UE
        List<int> UECountry = ReportsManager.Instance.GetUECountriesIDs();
        if (UECountry.Count > 0)
        {
            script.Append("UECountries = new Array(");
            for (int i = 0; i < UECountry.Count; i++)
            {
                script.Append("'");
                script.Append(UECountry[i]);
                script.Append("', ");
            }
            script.Remove(script.Length - 2, 2);
            script.Append("); ");
        }

        script.Append("baseCountry = '");
        script.Append(Settings.DefaultCountry);
        script.Append("'; ");
        script.Append(" lblNoShipmentAvailable = window.document.getElementById('");
        script.Append(lblNoShipment.ClientID);
        script.Append("'); ");

        //ustawianie tablic potrzebnych dla przełączania kosztów przesyłki
        Panel panel = null;
        int index = 0;
        int lblIndex = 2;
        int hfIndex = 4;
        for (int i = 0; i < pnlPayments.Controls.Count; i++)
        {
            if ((panel = pnlPayments.Controls[i] as Panel) != null)
            {
                script.Append(string.Format("rbtArray[{0}] = window.document.getElementById('{1}'); ", index, panel.ClientID));
                script.Append(string.Format("hfArray[{0}] = window.document.getElementById('{1}'); ", index, panel.Controls[hfIndex].ClientID));
                script.Append(string.Format("lblArray[{0}] = window.document.getElementById('{1}'); ", index, panel.Controls[lblIndex].ClientID));
                index++;
            }
        }

        script.Insert(0, string.Format("rbtArray = new Array({0}); hfArray = new Array({0}); lblArray = new Array({0}); ", index));

        //dodawanie akcji do przełączania sposobu płatności oraz płatności online
        List<ShipmentType> shipments = null;
        ListItem onlinePayment = null;
        foreach (ListItem item in rblPayments.Items)
        {
            if (item.Value != "0") //wszystkie z wyjątkiem online
            {
                shipments = OrderManager.Instance.GetShipmentPossibilities(int.Parse(item.Value), CommonTools.CurrentLanguageID);
                array.Append("new Array(");

                for (int i = 0; i < shipments.Count; i++)
                {
                    array.Append("'");
                    array.Append(shipments[i].ShipmentID);
                    array.Append("', ");
                }
                if (array[array.Length - 2] == ',')
                {
                    array.Remove(array.Length - 2, 2);
                }
                array.Append(")");

                item.Attributes.Add("onClick", string.Format("ShowPaymentForm(this);  SetShipment({0}, '{1}');", array.ToString(), ddl.ClientID));
                array.Remove(0, array.Length);
            }
            else
            {
                onlinePayment = item;
            }
        }

        HiddenField hf = null;
        RadioButton rbt = null;
        foreach (RepeaterItem item in rptOnlinePayments.Items)
        {
            hf = (HiddenField)item.FindControl("hfOP");
            rbt = (RadioButton)item.FindControl("rbOP");

            shipments = OrderManager.Instance.GetShipmentPossibilities(int.Parse(hf.Value), CommonTools.CurrentLanguageID);
            array.Append("new Array(");

            for (int i = 0; i < shipments.Count; i++)
            {
                array.Append("'");
                array.Append(shipments[i].ShipmentID);
                array.Append("', ");
            }
            if (array[array.Length - 2] == ',')
            {
                array.Remove(array.Length - 2, 2);
            }
            array.Append(")");

            rbt.Attributes["onClick"] += string.Format(" SetShipment({0}, '{1}'); ", array.ToString(), ddl.ClientID);

            if (onlinePayment != null)
            {
                onlinePayment.Attributes.Add("onClick", string.Format("ShowPaymentForm(this); SetUniqueRadioButton('rptOnlinePayments.*rbgOnlinePayments',{0}); SetShipment({1}, '{2}');", rbt.ClientID, array.ToString(), ddl.ClientID));
                onlinePayment = null;
            }
            array.Remove(0, array.Length);
        }

        //ustawienie odpowiednich opcji przesyłki zależnie od wybranego sposobu zapłaty
        string onClick = null;
        if (rblPayments.SelectedValue == "0") //płatności online
        {
            //wyszukanie zaznaczonej opcji płatności online
            foreach (RepeaterItem item in rptOnlinePayments.Items)
            {
                rbt = (RadioButton)item.FindControl("rbOP");
                if (rbt != null && rbt.Checked)
                {
                    onClick = rbt.Attributes["onClick"];
                    break;
                }
            }
        }
        else
        {
            onClick = rblPayments.SelectedItem.Attributes["onClick"];
        }
        if (onClick != null)
            script.Append(onClick.Substring(onClick.LastIndexOf("SetShipment")));

        //ustawanie akcji zmiany cen kosztów transportu podczas wyboru kraju
        ddl.Attributes.Add("onChange", "SetShipmentPrice(this);");

        //ustawienie cen podczas wgrywania strony
        script.Append("SetShipmentPrice(window.document.getElementById('");
        script.Append(ddl.ClientID);
        script.Append("')); ");

        CheckBox chb = null;
        //ustawienie resetowania kosztów przesyłki po zmianie adresu do wysyłki dla zalogowanego użytkownika 
        if (ctrlCustomerData.Mode == controls_ctrlCustomerData.ModeType.Address)
        {
            chb = (CheckBox)ctrlCustomerData.FindControl("chbOtherAddress");
            if (chb != null)
            {
                chb.Attributes["onClick"] += string.Format(" SetDefaultShipmentPrice(this, {0}, {1}); ", ddl.ClientID, Settings.DefaultCountry);
            }
        }

        //ustawienie resetowania kosztów przesyłki po zmianie opcji wysyłki za granicę
        chb = (CheckBox)ctrlCustomerData.FindControl("chbAbroad");
        if (chb != null)
        {
            chb.Attributes["onClick"] += string.Format(" SetDefaultShipmentPrice(this, {0}, {1});", ddl.ClientID, Settings.DefaultCountry);
        }


        chbRate.Attributes.Add("onClick", "ChbRateClick(this);");

        //startowy skrypt ustawiajacy wszystkie zmienne
        ClientScript.RegisterStartupScript(this.GetType(), "StartScript", string.Format(
            "<script type=\"text/javascript\">SetElementOnLoad(\"{0}\", \"{1}\");" +
            "PaymentFormVisibility({2}); {3}</script>", btnConfirm.ClientID, chbRate.ClientID, rblPayments.SelectedValue, script.ToString()));
    }

    private void ShowError(string message)
    {
        lblError.Text = message;
        lblError.Visible = true;
    }

    private void CreateShipmentList()
    {
        HtmlInputRadioButton radio = null;
        Label label = null;
        Panel panel = null;
        HiddenField hiddenField = null;
        List<ShipmentType> shipments = OrderManager.Instance.GetShipmentTypes(CommonTools.CurrentLanguageID);
        foreach (ShipmentType type in shipments)
        {
            panel = new Panel();
            panel.ID = "pnlShipment_" + type.ShipmentID.ToString();
            panel.Attributes.Add("value", type.ShipmentID.ToString());
            radio = new HtmlInputRadioButton();
            radio.Name = "ShipmentList";
            radio.ID = "rbShipment_" + type.ShipmentID.ToString();
            radio.Value = type.ShipmentID.ToString();
            panel.Controls.Add(radio);
            panel.Controls.Add(new LiteralControl(type.ShipmentName + "("));
            label = new Label();
            label.Text = type.CountryShipmentPrice.ToString();
            panel.Controls.Add(label);
            panel.Controls.Add(new LiteralControl(Resources.Language.Currency + ")"));

            hiddenField = new HiddenField();
            hiddenField.Value = string.Format("{0}${1}${2}", TranslateShipmentPrice(type.CountryShipmentPrice), TranslateShipmentPrice(type.EUShipmentPrice), TranslateShipmentPrice(type.WorldShipmentPrice));
            panel.Controls.Add(hiddenField);

            pnlPayments.Controls.Add(panel);
        }
    }

    private string TranslateShipmentPrice(decimal price)
    {
        if (price < 0)
        {
            return "X";
        }
        else
        {
            return price.ToString("F02");
        }
    }

    private bool ValidateVoucher()
    {
        lbVoucherError.Visible = false;
        string code;
        Voucher voucher;
        if (!string.IsNullOrEmpty(tbVoucher.Text))
        {
            if (tbVoucher.Text.Length == 9)
            {
                code = tbVoucher.Text.Replace("-","").Substring(0,8);
                voucher = OrderManager.Instance.GetVoucherByGuid(code,false);
                if (voucher != null)
                {
                    return true;
                }
                else
                {
                    lbVoucherError.Visible = true;
                    return false;
                }
            }
            else
            {
                lbVoucherError.Visible = true;
                return false;
            }
        }
        else return true;
    }

    #endregion METHODS

    #region EVENTS

    protected void btnConfirm_Click(object sender, EventArgs e)
    {
        decimal totalAmountNetto = 0;
        decimal totalAmountBrutto = 0;
        bool acceptZagiel = chbRate.Checked;

        Realization order = new Realization();

        if (!ValidateVoucher())
        {
            return;
        }

        if (!string.IsNullOrEmpty(tbVoucher.Text))
        {
            string code;
            Voucher voucher;

            if (tbVoucher.Text.Length == 9)
            {
                code = tbVoucher.Text.Replace("-", "").Substring(0, 8);
                voucher = OrderManager.Instance.GetVoucherByGuid(code, false);
                if (voucher != null)
                {
                    order.Voucher = voucher;
                }
            }
        }

        if (Page.User.Identity.IsAuthenticated && !ctrlCustomerData.OtherAddress)
        {
            MembershipUser user = Membership.GetUser(true);
            order.iCustomer = OrderManager.Instance.GetCustomerByUserId((Guid)user.ProviderUserKey, CommonTools.CurrentLanguageID);
        }
        else
        {
            if (!(ValidateUser() && ValidateShipmentType()))
            {
                return;
            }

            GetPaymentTypes();

            order.iCustomer = new Customer();
            order.iCustomer.FirstName = ctrlCustomerData.Name;
            order.iCustomer.Surname = ctrlCustomerData.Surname;
            order.iCustomer.Street = ctrlCustomerData.Street + " " + ctrlCustomerData.Building + "/" + ctrlCustomerData.Flat;
            order.iCustomer.City = ctrlCustomerData.City;
            order.iCustomer.ZipCode = ctrlCustomerData.PostCode1;// +"-" + ctrlCustomerData.PostCode2;
            order.iCustomer.CountryID = ctrlCustomerData.CountryID;
            order.iCustomer.Country = ctrlCustomerData.CountryName;
            order.iCustomer.Phone = ctrlCustomerData.Phone;
            order.iCustomer.Email = ctrlCustomerData.Email;
            order.iCustomer.Password = ctrlCustomerData.Password;
            order.iCustomer.Question = ctrlCustomerData.Question;
            order.iCustomer.Answer = ctrlCustomerData.Answer;
            order.iCustomer.IsCompany = ctrlCustomerData.IsInvoice;
            order.iCustomer.CompanyStreet = ctrlCustomerData.CompanyStreet + " " + ctrlCustomerData.CompanyBuilding + "/" + ctrlCustomerData.CompanyFlat;
            order.iCustomer.CompanyCity = ctrlCustomerData.CompanyCity;
            order.iCustomer.CompanyName = ctrlCustomerData.CompanyName;
            order.iCustomer.CompanyNIP = ctrlCustomerData.NIP;
            order.iCustomer.CompanyZipCode = ctrlCustomerData.CompanyPostCode1;// + "-" + ctrlCustomerData.CompanyPostCode2;
            order.iCustomer.SameAddress = ctrlCustomerData.SameAddress;
            order.iCustomer.PreferedLanguageID = ctrlCustomerData.LanguageID;
            order.iCustomer.NewsSubscription = ctrlCustomerData.Newsletter;
        }
        order.OtherAddress = ctrlCustomerData.OtherAddress;


        order.SendAbroad = ctrlCustomerData.SendAbroad;
        order.Info = tbAdditionalInfo.Text;

        order.PaymentID = int.Parse(rblPayments.SelectedValue);
        if (order.PaymentID == 0)
            order.PaymentID = GetSelectedOnlinePayment();
        order.Payment = OrderManager.Instance.GetPaymentType(order.PaymentID, CommonTools.CurrentLanguageID);

        order.ShipmentID = int.Parse(ShipmentSelectedValue);
        order.Shipment = OrderManager.Instance.GetShipmentType(order.ShipmentID, CommonTools.CurrentLanguageID);

        if (order.SendAbroad)
        {
            if (ReportsManager.Instance.GetCountryNameById(order.iCustomer.CountryID, CommonTools.CurrentLanguageID).MemberUE)
            {
                order.SendAbroadType = AbroadSend.UE;
                order.ShipmentCost = order.Shipment.EUShipmentPrice;
            }
            else
            {
                order.SendAbroadType = AbroadSend.OutUE;
                order.ShipmentCost = order.Shipment.WorldShipmentPrice;
            }
        }
        else
        {
            order.SendAbroadType = AbroadSend.None;
            order.ShipmentCost = order.Shipment.CountryShipmentPrice;
        }

        Session["OrderData"] = order;

        if (this.IsValid && ((order.Payment.PaymentGroup == PaymentGroups.Instalment && acceptZagiel) || order.Payment.PaymentGroup != PaymentGroups.Instalment))
        {
            Dictionary<int, BasketItem> basket = (Dictionary<int, BasketItem>)Session["BasketItems"];
            
            foreach (BasketItem item in basket.Values)
            {
                totalAmountNetto += item.PriceNetto * item.Count;
                totalAmountBrutto += item.PriceBrutto * item.Count;
            }

            order.TotalAmountNetto = totalAmountNetto;
            order.TotalAmountBrutto = totalAmountBrutto;

            if (order.Voucher != null && order.Voucher.AcceptValue > order.TotalAmountBrutto)
            {
                lbOrderValToLow.Visible = true;
                lbOrderValToLow.Text = string.Format(Resources.Language.OrderValueToLow, order.Voucher.AcceptValue.ToString("F2")+Resources.Language.Currency); 
                return;
            }

            if (order.Voucher != null)
            {
                switch (order.Voucher.Type)
                {
                    case VoucherType.FreeShipment:
                        {
                            order.ShipmentCost = 0;
                            break;
                        }
                    case VoucherType.Percent:
                        {
                            order.TotalAmountBrutto = (order.TotalAmountBrutto - (order.TotalAmountBrutto * (order.Voucher.Value.Value / 100m)));
                            //order.TotalAmountNetto = (order.TotalAmountNetto - (order.TotalAmountNetto * (order.Voucher.Value.Value / 100m)));
                            break;
                        }
                    case VoucherType.Value:
                        {
                            order.TotalAmountBrutto -= order.Voucher.Value.Value;
                            break;
                        }
                }
            }

            Session["OrderData"] = order;

            Response.Redirect(Resources.Language.UrlOrderConfirmation.ToLower() + ".aspx");
        }
        else if (!acceptZagiel)
        {
            lblRateAcceptFault.Visible = true;
        }
    }

    protected void rptOnlinePayments_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
            return;
        RadioButton rdo = (RadioButton)e.Item.FindControl("rbOP");
        string script = "SetUniqueRadioButton('rptOnlinePayments.*rbgOnlinePayments',this);";
        rdo.Attributes.Add("onclick", script);
        Image img = (Image)(e.Item.FindControl("imgPO1"));
        if (img.ImageUrl.Contains(";"))// jeżeli są dwa obrazki dla jednej płatności
        {
            string[] tmp = img.ImageUrl.Split(';');
            img.ImageUrl = tmp[0];
            ((Image)(e.Item.FindControl("imgPO2"))).Visible = true;
            ((Image)(e.Item.FindControl("imgPO2"))).ImageUrl = tmp[1];
        }
    }

    #endregion EVENTS

    #region PAGE EVENTS

    protected void Page_Load(object sender, EventArgs e)
    {
        lbOrderValToLow.Visible=false;
        if (!IsPostBack)
        {
            GetPaymentTypes();

            rblPayments.Items.Clear();
            rblPayments.DataSource = ActivePayments;
            rblPayments.DataBind();
            rblPayments.Items.Add(new ListItem(Resources.Language.OnlinePayment, "0", true));

            rptOnlinePayments.DataSource = ActiveOnlinePayments;
            rptOnlinePayments.DataBind();

            rblPayments.Items[0].Selected = true;
            lblNoShipment.Style[HtmlTextWriterStyle.Display] = "none";
        }

        CreateShipmentList();

        if (User.Identity.IsAuthenticated)
        {
            ctrlCustomerData.Mode = controls_ctrlCustomerData.ModeType.Address;
        }
        else
        {
            ctrlCustomerData.Mode = controls_ctrlCustomerData.ModeType.NewAccount;
        }

    }

    protected void Page_LoadComplete(object sender, EventArgs e)
    {
        if (Session["OrderData"] != null)
        {
            Realization order = (Realization)Session["OrderData"];
            ctrlCustomerData.Building = order.iCustomer.Street.Split('/', ' ')[1];
            ctrlCustomerData.City = order.iCustomer.City;
            ctrlCustomerData.CountryID = order.iCustomer.CountryID;
            ctrlCustomerData.Email = order.iCustomer.Email;
            try
            {
                ctrlCustomerData.Flat = order.iCustomer.Street.Split('/', ' ').Length>=3 ? order.iCustomer.Street.Split('/', ' ')[2] : string.Empty;
            }
            catch { ctrlCustomerData.Flat = string.Empty; }
            ctrlCustomerData.Name = order.iCustomer.FirstName;
            ctrlCustomerData.Phone = order.iCustomer.Phone;
            ctrlCustomerData.PostCode1 = order.iCustomer.ZipCode;//.Split('-')[0];
            //ctrlCustomerData.PostCode2 = order.iCustomer.ZipCode.Split('-')[1];
            ctrlCustomerData.Street = order.iCustomer.Street.Split('/', ' ')[0];
            ctrlCustomerData.Surname = order.iCustomer.Surname;
            ctrlCustomerData.SendAbroad = order.SendAbroad;
            ctrlCustomerData.Newsletter = order.iCustomer.NewsSubscription; 

            if (order.Payment.PaymentID <= 3)
                rblPayments.SelectedValue = order.Payment.PaymentID.ToString();
            else
            {
                rblPayments.SelectedValue = "0";

                SelectOnlinePaymentType(order.Payment.PaymentID);
            }

            ctrlCustomerData.IsInvoice = order.iCustomer.IsCompany;
            if (order.iCustomer.IsCompany)
            {
                ctrlCustomerData.CompanyBuilding = order.iCustomer.CompanyStreet.Split('/', ' ')[1];
                ctrlCustomerData.CompanyCity = order.iCustomer.CompanyCity;
                ctrlCustomerData.CompanyFlat = (order.iCustomer.Street.Split('/', ' ').Length>=3)?order.iCustomer.Street.Split('/', ' ')[2]:"";
                ctrlCustomerData.CompanyName = order.iCustomer.CompanyName;
                ctrlCustomerData.CompanyPostCode1 = order.iCustomer.CompanyZipCode;//.Split('-')[0];
                //ctrlCustomerData.CompanyPostCode2 = order.iCustomer.CompanyZipCode.Split('-')[1];
                ctrlCustomerData.CompanyStreet = order.iCustomer.CompanyStreet.Split('/', ' ')[0];
                ctrlCustomerData.SameAddress = order.iCustomer.SameAddress;
                ctrlCustomerData.NIP = order.iCustomer.CompanyNIP;
            }
            else
            {
                ((RadioButton)(rptOnlinePayments.Items[0].FindControl("rbOP"))).Checked = true;
            }

            if (order.Voucher != null)
            {
                tbVoucher.Text = order.Voucher.Code.ToString();
            }
        }
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        AddAttributesAndScripts();
    }

    #endregion PAGE EVENTS
}
